package com.vgooo.client.vchannel;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vgooo.bean.NewsPing;
import com.vgooo.client.R;
import com.vgooo.util.BitmapUtils;
import com.vgooo.util.TimeUtils;

public class PingAdapter extends BaseAdapter {
	public List<NewsPing> newsPings;
	private LayoutInflater inflater;
	private Context context;
	private View titleView  ;
	private boolean hasTitle = false ;

	public PingAdapter(List<NewsPing> newsPings, Context context) {
		super();
		setPings(newsPings);
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.hasTitle = false ;
	}
	public PingAdapter(View titleView ,List<NewsPing> newsPings, Context context) {
		super();
		this.setTitleView(titleView);
		this.setPings(newsPings);
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.titleView = titleView ;
	}
	

	public void setTitleView(View titleView) {
		if(null!=titleView){
			this.titleView = titleView;
			this.hasTitle = true ;
		}else{
			this.hasTitle = false ;
		}
	}
	public void setPings(List<NewsPing> newsPings) {
		if (newsPings != null && !newsPings.isEmpty()) {
			this.newsPings = newsPings;
//			log("setPings : " + newsPings);
		} else {
			newsPings = new ArrayList<NewsPing>();
//			log("setPings : 评论为空。。。。。 " + newsPings);
		}

	}
	/**
	 * 增加数据
	 * @param newsPings
	 */
	public void adddata(List<NewsPing> newsPings){
		if(newsPings!= null && !newsPings.isEmpty()){
			this.newsPings.addAll(newsPings);
//			log("pings = " + newsPings);
			notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(newsPings==null||newsPings.isEmpty()){
			return 0 ;
		}
		if(hasTitle){
			return newsPings.size()+1;
		}else{
			return newsPings.size();
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return newsPings.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if(!hasTitle){
			return 0 ;
		}
		switch (position) {
		case 0:
			return 0 ;

		default:
			return 1 ;
		}
	}
	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		if(hasTitle){
			return 2 ;
		}else{
			return 1;
		}
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(hasTitle){
			switch (position) {
			case 0:
				return titleView ;
			default:
				position--;
				break;
			}
		}
		NewsPing newsPing = newsPings.get(position);
//		log(position+1+"、" + newsPing.user_name+":  "+newsPing);
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.ping_item, null);
			holder = new ViewHolder() ;
			convertView.setTag(holder);
			holder.photo = (ImageView) convertView.findViewById(R.id.pring_item_image);
			holder.name = (TextView) convertView.findViewById(R.id.ping_item_name);
			holder.time = (TextView) convertView.findViewById(R.id.ping_item_time);
			holder.content = (TextView) convertView.findViewById(R.id.ping_item_content);
			holder.layout = (RelativeLayout) convertView.findViewById(R.id.ping_layout_quote) ;
			holder.parrentNickName = (TextView) convertView.findViewById(R.id.ping_layout_quote_parent_name);
			holder.parrentContent = (TextView) convertView.findViewById(R.id.ping_layout_quote_content);
			holder.quote = (ImageView) convertView.findViewById(R.id.ping_item_quote);
			
		}else{
			holder = (ViewHolder) convertView.getTag() ;
		}

		BitmapUtils.loaderImage(newsPing.user_img, holder.photo,context);
		holder.name.setText(newsPing.user_name);
//		if(newsPing.feedback_time.matches("^[0-9]+$")){
//			holder.time.setText(newsPing.feedback_time);
//		}else{
//			holder.time.setText(newsPing.feedback_time);
//		}
		holder.time.setText(newsPing.feedback_time);
		holder.content.setText(newsPing.feedback_content);
		if(newsPing.parent_id==null||"0".equals(newsPing.parent_id)){
			holder.layout.setVisibility(View.GONE);
		}else{
			holder.layout.setVisibility(View.VISIBLE);
			holder.parrentNickName.setText(newsPing.parent.user_name);
			holder.parrentContent.setText(Html.fromHtml("<font color=\"#73AA23\">“ </font>"+newsPing.parent.feedback_content+"<font color=\"#73AA23\"> ”</font>"));
		}
		holder.quote.setTag(newsPing.feedback_id);
		if(position%10==0){
			BitmapUtils.cleanCache();
		}
		return convertView;
	}
	
	
	class ViewHolder{
		ImageView photo ;
		TextView name ;
		TextView time ;
		TextView content ;
		ImageView quote ;
		RelativeLayout layout ;
		TextView parrentNickName ;
		TextView parrentContent ;
	}
	/**
	 * 打印调试信息
	 * 
	 * @param info
	 */
	private void log(String info) {
		Log.i(this.getClass().getSimpleName(), info);
	}
	
	

}
